package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.WaiterDao;
import entiey.Waiter;
import util.JDBCUtil;
import util.ResultSetCreateObject;

public class WaiterDaoImpl implements WaiterDao,ResultSetCreateObject<Waiter>{

	@Override
	public int add(Waiter t) {
		int num = 0;
		String sql = "insert into waiter(waiterNo,name,gender,phone,inDate,moreInfo) values(?,?,?,?,?,?) ";
		num = JDBCUtil.executeUpdate(sql,t.getWaiterNo(),t.getName(),t.getGender(),t.getPhone(),t.getTime(),t.getMoreInfo());
		
		
		return num;
	}

	@Override
	public int delete(int no) {
		int num = 0;
		String sql = "delete from waiter where id = ? ";
		num = JDBCUtil.executeUpdate(sql,no);
		
		
		return num;
	}

	@Override
	public int update(Waiter t) {
		int num = 0;
		String sql = "update waiter set waiterNo=?,name=?,gender=?,phone=?,inDate=?,moreInfo=?  where id =?";
		num = JDBCUtil.executeUpdate(sql,t.getWaiterNo(),t.getName(),t.getGender(),t.getPhone(),t.getTime(),t.getMoreInfo(),t.getId());
		
		
		return num;
	}

	@Override
	public List<Waiter> findAll() {
		List<Waiter> list = new ArrayList<Waiter>();
		String sql = "select * from waiter";
		list = JDBCUtil.executeQuery(sql, this);
		
		return list;
	}

	
	//通过工号查询
	@Override
	public Waiter findById(int waiterNo) {
		List<Waiter> list = new ArrayList<Waiter>();
		String sql = "select * from waiter where waiterNo = ?";
		list = JDBCUtil.executeQuery(sql,this,waiterNo);
		Waiter one = null;
		if(list.size()>0) {
			one =list.get(0);
		}
		return one;
	}

	@Override
	public Waiter create(ResultSet rs) {
		Waiter waiter = new Waiter();
		try {
			waiter.setId(rs.getInt("id"));
			waiter.setWaiterNo((rs.getInt("waiterNo")));
			waiter.setName(rs.getString("name"));
			waiter.setGender(rs.getString("gender"));
			waiter.setPhone(rs.getString("phone"));
			waiter.setTime(rs.getDate("inDate"));
			waiter.setMoreInfo(rs.getString("moreInfo"));
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return waiter;
	}

	@Override
	public List<Waiter> findByName(String name) {
		
		List<Waiter> list = new ArrayList<Waiter>();
		String sql = "select * from waiter where name like ?";
		
		list = JDBCUtil.executeQuery(sql,this,"%"+name+"%");
		
		return list;
	}

}
